/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.bomt.domain.vo;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.comac.ins.bomt.domain.DetailStrengthBom;
import com.comac.ins.bomt.domain.DetailStrengthV2Bom;
import com.comac.ins.bomt.utils.BomVersionUtil;
import com.comac.ins.common.core.utils.ValidatorUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;

import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * The type Detail strength bom vo.
 *
 * @author zhanghongyi
 * @date 2024 /8/5 16:08
 * @description DetailStrengthBomVo
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AutoMapper(target = DetailStrengthV2Bom.class)
public class DetailStrengthV2BomVo {

    /**
     * Id
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long id;

    /**
     * 清单Id
     */
    private Long infoId;
    /**
     * 标准装机清单明细Id
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long standardDetailId;


    /**
     * 原始装机清单明细Id
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long originalDetailId;
    /**
     * 版本数
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String versionNum;

    /**
     * 机型
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String aircraftModel;

    /**
     * 跟踪Id（用于标记产出数据的业务流程Id）
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String track;

    /**
     * 子跟踪Id（用于标记产出的业务流程步骤）
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String subTrack;
    /**
     * ATA 100
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String ata;

    /**
     * 主分组类型
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String groupType;

    /**
     * 子分组类型
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String subGroupType;
    /**
     * 注释
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String note;
    /**
     * 名称
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ExcelProperty(index = 3)
    private String name;

    /**
     * 层级
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ExcelProperty(index = 0)
    private String level;

    /**
     * 全路径图号
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String fullCodePath;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String groupCode;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String groupName;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String groupVersion;

    /**
     * 图号
     */
    @ExcelProperty(index = 1)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String code;

    /**
     * 版次
     */
    @ExcelProperty(index = 2)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String version;

    /**
     * 顺序
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long ord;


    /**
     * 导出强度数模清单
     *
     * @param outputStream the output stream
     * @param sheetName    the sheet name
     * @param data         the data
     */
    public static void exportStrengthBom(OutputStream outputStream, String sheetName, List<DetailStrengthV2BomVo> data) {
        //准备head
        List<String> heads = Lists.newArrayList();
        heads.addAll(Arrays.asList("层级", "图号", "名称"));
        List<String> versions = BomVersionUtil.getVersions(data);
        heads.addAll(versions);
        //准备数据
        List<List<Object>> toBeExportData = Lists.newArrayList();
        for (DetailStrengthV2BomVo vo : data) {
            List<Object> row = Lists.newArrayList();
            row.add(vo.getLevel());
            row.add(vo.getCode());
            row.add(vo.getName());
            for (String version : versions) {
                if (StringUtils.equals(version, vo.getVersion())) {
                    row.add(vo.getVersionNum());
                } else {
                    row.add(null);
                }
            }
            toBeExportData.add(row);
        }
        //进行导出
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteCellStyle.setWriteFont(headWriteFont);

        try (ExcelWriter writer = EasyExcelFactory.write(outputStream).autoCloseStream(false).registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, (WriteCellStyle) null)).build()) {
            List<List<String>> headTitles = Lists.newArrayList();
            for (String head : heads) {
                headTitles.add(List.of(head));
            }
            WriteSheet incomeSheet = new WriteSheet();
            incomeSheet.setHead(headTitles);
            incomeSheet.setSheetName(sheetName);

            writer.write(toBeExportData, incomeSheet);
            writer.finish();
        }
    }
    /**
     * 参数检查
     */
    public void checkParam() {
        ValidatorUtils.validate(this);
    }
}
